package com.dc.bobaoge.project.v2.to.v3.business.core.method.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreUserMapper;
import com.own.business.sync.trigger.base.entity.SyncTrigger;
import com.own.business.sync.trigger.base.method.ResolveMethod;
import com.own.business.system.account.common.em.AccountType;
import com.own.business.system.account.entity.SystemAccount;
import com.own.business.system.account.mapper.SystemAccountMapper;
import com.own.business.system.user.mapper.SystemUserMapper;
import com.own.component.common.exception.BusinessSimpleException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * UserUpdatePasswordResolveMethod
 *
 * @author chenxueli
 * @date 2024-01-16 14:26:00
 */
@Slf4j
@Component
public class UserUpdatePasswordResolveMethod implements ResolveMethod {

    @Resource
    private OldCoreUserMapper oldCoreUserMapper;

    @Resource
    private SystemUserMapper systemUserMapper;
    @Resource
    private SystemAccountMapper systemAccountMapper;

    /**
     * 修改的处理方法
     *
     * @param trigger 日志信息
     */
    @Override
    public void update(SyncTrigger trigger) {
        // 查询用户信息是否存在
        var oldCoreUser = oldCoreUserMapper.selectById(trigger.getTargetId());
        if (oldCoreUser == null) {
            throw new BusinessSimpleException("用户信息不存在");
        }
        // 查询是否已经存在当前用户了
        var systemUser = systemUserMapper.selectById(oldCoreUser.getUserId());
        if (systemUser == null) {
            throw new BusinessSimpleException("用户信息不存在");
        }
        // 判断是否存在账号密码的账号
        var wrapper = new LambdaQueryWrapper<SystemAccount>()
                .eq(SystemAccount::getFkUserId, systemUser.getId())
                .eq(SystemAccount::getType, AccountType.TEXT.type());
        var account = systemAccountMapper.selectOne(wrapper);
        if (account == null) {
            // 新增
            account = new SystemAccount();
            var accountName = StringUtils.isNotBlank(oldCoreUser.getUserAccount()) ? oldCoreUser.getUserAccount() : oldCoreUser.getUserPhone();
            account.setAccount(accountName);
            account.setPassword(oldCoreUser.getUserPwd());
            account.setType(AccountType.TEXT.type());
            account.setFkUserId(systemUser.getId());
            systemAccountMapper.insert(account);
        } else {
            // 直接修改信息
            systemAccountMapper.update(new LambdaUpdateWrapper<SystemAccount>()
                    .eq(SystemAccount::getFkUserId, systemUser.getId())
                    .eq(SystemAccount::getType, AccountType.TEXT.type())
                    .set(SystemAccount::getPassword, oldCoreUser.getUserPwd()));
        }

    }

    /**
     * 名称信息
     *
     * @return 名称
     */
    @Override
    public String name() {
        return "user_password";
    }

}
